<?php
namespace Tbf\Bundle\TestBundle\SetUp;

class SetupCollection{
    protected $setup_list;
    function addSetup($name,SetupInterface $setup){
        $this->setup_list[$name] = $setup;
    }

    /**
     * @return SetupInterface[]
     */
    function all(){
        return $this->setup_list;
    }

    /**
     * @param $name
     * @return SetupInterface
     */
    function get($name){
        return $this->setup_list[$name];
    }

    /**
     * @param $name
     * @return bool
     */
    function has($name){
        return !empty($this->setup_list[$name]);
    }

    /**
     * 获取某个setup的所有依赖名
     * @param $name
     * @return array []string
     */
    function resolveDepsByName($setup_name,&$visited=array()){
        if (!$this->has($setup_name)){
            throw new \Exception(sprintf('you request a non exist deps: %s',$setup_name));
        }
        $visited[$setup_name] = 1;
        $this_setup = $this->get($setup_name);
        $output = array();
        foreach($this_setup->getDeps() as $deps){
            if (!empty($visited[$setup_name])){
                continue;
            }
            $output = array_merge($this->resolveDepsByName($deps,$visited),$output);
        }
        return $output;
    }
}